import React, {useEffect, useRef, useState} from 'react'
import {FormGroup, Input} from "reactstrap";
import {SET_RELEASE_FILTER_CHECKED} from "../../../redux/constants/releases";
import {Search} from "react-feather";

const ReleaseSearch = props => {
  let searchText = ''
  const searchFormRef = useRef();
  const searchHandleChange = e => {
    searchText = e.target.value;
    setTimeout(() => {
      props.setFieldData(SET_RELEASE_FILTER_CHECKED, "search", searchText)
    }, 200)
  }

  useEffect(() => {
    if (!props.userFilterData.search && searchFormRef)
      searchFormRef.current.reset()
  }, [props.userFilterData])

  return (
    <div className="ecommerce-searchbar releases__search">
      <FormGroup className="position-relative">
        <form ref={searchFormRef}>
          <Input
            className="search-product"
            placeholder="Попробуй поискать здесь"
            onChange={searchHandleChange}
          />
        </form>
        <div className="form-control-position">
          <Search size={22}/>
        </div>
      </FormGroup>
    </div>
  )
}

export default ReleaseSearch
